import { adminRouterMap, userRouterMap, constantRouterMap } from '@/router'
import user from './user'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(roles, route=[]) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRouter(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}
/**
 *将后台的路由表进行格式化
 * @param {*} asyncRouterMap
 */
function convertRouter(asyncRouterMap) {
    const accessedRouters = []
    if (asyncRouterMap) {
        asyncRouterMap.forEach(item => {
            let parent = generateRouter(item)
            accessedRouters.push(parent)
        })
    }
    accessedRouters.push({ path: '*', redirect: '/404', hidden: true })
    return accessedRouters
}
function generateRouter(item) {
  var children = []
  if (item.children !=null && item.children.length>0) {
    item.children.forEach(child => {
      children.push(generateRouter(child))
    })
  }
  if(children.length>0) item.children = children
  let component = item.component || '/layout/Layout'
  item.component = () => import('@/views'+ component)
    //item.component = componentsMap[item.component]
    // var router = {
    //   path: item.path,
    //   name: item.name,
    //   meta: item.meta,
    //   // component: isParent ? Layout : () => import(item.component)
    //   component: componentsMap[item.name]
    // }
    return item
}
const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers=[]) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({commit }, data) {
      return new Promise(resolve => {
        // const { serverRouterMap } = data
        let accessedRouters
        console.log('user.state.roles',user.state.roles,user.state)
        switch (user.state.roles) {
          case "ADMIN":
            accessedRouters = adminRouterMap;
            break;
          case "USER":
            accessedRouters = userRouterMap;
            break;
        }

        // if (roles.includes('admin')) {
        //   accessedRouters = asyncRouterMap
        // } else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        //   console.log("asyncRouterMap",asyncRouterMap);
        //   console.log("serverRouterMap",serverRouterMap);
        //   console.log("user",user);

          // accessedRouters = asyncRouterMap;
          // if(serverRouterMap && serverRouterMap.length > 0)
          //     accessedRouters = convertRouter(serverRouterMap)
          // console.log("accessedRouters",accessedRouters)
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
